package com.snail.auth.action;

import com.common.ResultUtil;
import com.common.entity.BaseController;
import com.common.entity.Page;
import com.snail.auth.entity.AuthRole;
import com.snail.auth.service.AuthRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "角色维护")
@RestController
@RequestMapping("/auth/role")
public class AuthRoleController extends BaseController {

    @Autowired
    private AuthRoleService roleService;

    @GetMapping(value = "/page")
    public Map<String, Object> page(Page page) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", request.getParameter("keyword"));
        page = roleService.page(page, params);
        return ResultUtil.result(page);
    }

    @GetMapping(value = "/{id}")
    public Map<String, Object> input(@PathVariable Long id) {
        AuthRole entity = roleService.getByKey(id);
        entity.setAuthorities(null);
        return ResultUtil.result(entity);
    }

    @PostMapping
    public Map<String, Object> save(AuthRole entity) {
        entity.setFlag(0);
        roleService.save(entity);
        return ResultUtil.result(entity);
    }

    @DeleteMapping(value = "/{id}")
    public Map<String, Object> del(@PathVariable Long id) {
        roleService.del(id);
        return ResultUtil.result();
    }

    @GetMapping(value = "/tree")
    public Map<String, Object> tree() {
        return ResultUtil.result(roleService.treeData());
    }

}
